#!/bin/env python
#coding:utf-8
__author__ = 'mason'

import sys
import platform
if platform.system() == "Linux":
    sys.path.append("/usr/local/webserver/learn_django")
# 导入django Model
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'learn_django.settings'
import django
django.setup()
from grafanaCallBack.models import BusinessGroup, TeamUser, PackageName
# 导入logging
import logging
logger = logging.getLogger(__name__)
# 邮件相关
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

from grafanaCallBack.conf import setting
from grafanaCallBack.qiniu import apiPartialSearch, sendEmailClass
import collections, time, pickle, datetime, json

beginTime = time.time()
emallHost = setting.configs[u'emallHost']
emallUser = setting.configs[u'emallUser']
emallPasswd = setting.configs[u'emallPasswd']
toList = [u'mason@yunlai.cn', u'william@yunlai.cn']
# toList = [u'mason@yunlai.cn', u'william@yunlai.cn', \
#           u'rain@yunlai.cn', \
#           u'kevin@yunlai.cn', u'ladd@yunlai.cn', \
#           u'plum@yunlai.cn', \
#           u'lemon@yunlai.cn', \
#           u'eyden@yunlai.cn']

appLicationReponame = setting.configs[u'applicationReponame']
nginxAccessReponame = setting.configs[u'nginxAccessReponame']
nginxErrorReponame = setting.configs[u'nginxErrorReponame']


startTime = datetime.datetime.combine(datetime.date.today(),datetime.time.min)
endTime = datetime.datetime.combine(datetime.date.today(),datetime.time.max)    #得到datetime对象类型的时间
startTime = time.mktime(startTime.timetuple())
endTime = time.mktime(endTime.timetuple())      #得到时间戳类型的时间
startTime = int(startTime*1000)
endTime = int(endTime*1000)     #转化为java语言的时间戳(因为java是用毫秒，python是用秒)
print ('startTime is:',startTime,' ','endTime is:',endTime)
size = 5000

#邮件附件
def addimg(src, imgid):
    fp = open(src, "rb")
    msgImage = MIMEImage(fp.read())
    fp.close()
    msgImage.add_header('Content-ID', imgid)
    return msgImage

def get_queryCondition(queryString, startTime, endTime, size):
    #queryString = 'datasource:"%s" AND loglevel:"ERROR"' % (packageAbsLogPath)
    print ('查询语句为：',queryString)
    queryString = {
       "query_string":queryString,
        "sort": 'time_local',
        "size": size,
        "startTime": startTime,
        "endTime": endTime,
        "searchType": 1,
    }
    print ('查询语句为：', queryString)
    return queryString



attFilePath = []
allHtmlSummary =  '<div>'
businessGroup = BusinessGroup.objects.all()
for group in businessGroup:
    allHtmlSummary = allHtmlSummary + '<h4>%s</h4>' % group.chinese_name
    htmlSummary = '<table border=1><tr><td>应用名称</td><td>错误日志条数</td></tr>'
    print ('business group is:',group, '*'*100)
    #pickleFile = group.name + str(datetime.date.today()) + '.pk'
    htmlFileName = group.english_name +'_'+ str(datetime.date.today()) +'.html'
    currentDir = os.getcwd()
    absHtmlFileName =  os.path.join(currentDir, 'htmlFile', htmlFileName)
    print ('生成HTML文件的绝对路径为:',absHtmlFileName)
    if os.path.isfile(absHtmlFileName):
        os.remove(absHtmlFileName)
    packageAbsLogPath = BusinessGroup.objects.get(id=group.id).packagename_set.all()
    print ("开始从七牛云查询应用的日志:",'*'*20)
    if group.english_name == 'YunWeiTeam_devops':
        ## 异常状态码的nginx的access日志处理
        htmlFileName = 'nginxAccessLog' + '_' + str(datetime.date.today()) + '.html'    #针对异常状态码的nginx的access日志重命名
        currentDir = os.getcwd()
        absHtmlFileName = os.path.join(currentDir, 'htmlFile', htmlFileName)
        print ('生成HTML文件的绝对路径为:', absHtmlFileName)
        if os.path.isfile(absHtmlFileName):
            os.remove(absHtmlFileName)
        for i in packageAbsLogPath:
            packageDic = collections.OrderedDict()
            packageDic['name'] =  i
            queryString = 'datasource:"%s" AND status:>=400 AND NOT status:499' % (i)
            queryCondition = get_queryCondition(queryString, startTime, endTime, size)
            for k,v in apiPartialSearch.apiPartialSearch(nginxAccessReponame, queryCondition).items():
                packageDic[k] = v
            htmlSummary = htmlSummary + '<tr><td><a href="#%s">%s</a></td><td>%s</td></tr>' % (i, i, packageDic['total'])
            htmlDeatil = '<div id="%s" style="font-size:14px">' % i
            if packageDic['total'] > 0:
                htmlDeatil = htmlDeatil + '<h4>%s异常状态码日志明细(共产生<span style="color:red; font-weight:bold;">%s</span>条错误日志，注:如超过5000条则只显示前5000条)</h4><ul>' % (packageDic['name'], packageDic['total'])
                for row in packageDic['hits']:
                    lineLog = r'<li style="list-style: none; border-bottom: #ccc 1px dotted;">' \
                              '%s %s ' \
                              '%s %s %s ' \
                              '%s <span style="color:red;">%s</span> <span style="color:red;">%s</span> ' \
                              '%s <span style="color:red;">%s</span> %s ' \
                              '%s %s %s ' \
                              '%s %s ' \
                              '</li>' % (
                        str(datetime.datetime.strptime(row.get('time_local', '-')[0:19], '%Y-%m-%dT%H:%M:%S') + datetime.timedelta(hours=8)), row.get('client_ip', '-'), \
                        row.get('client_ip_Country', '-'), row.get('client_ip_Region', '-'), row.get('client_ip_City', '-'), \
                        row.get('request_method', '-'), row.get('uri'), row.get('args', '-'), \
                        row.get('server_protocol', '-'), row.get('status'), row.get('body_bytes_sent', '-'), \
                        row.get('http_referer', '-'), row.get('http_user_agent'), row.get('request_time', '-'), \
                        row.get('request_length', '-'), row.get('upstream_response_time', '-'),
                    )
                    htmlDeatil = htmlDeatil + lineLog
                htmlDeatil = htmlDeatil + '</ul></div>'
            else:
                htmlDeatil = htmlDeatil + '<h4>%s未产生异常状态码日志Good!!!</h4></div>' % (packageDic['name'])
            with open(absHtmlFileName, 'a') as f:
                f.write(htmlDeatil)
        htmlSummary = htmlSummary + '</table>'
        allHtmlSummary = allHtmlSummary + htmlSummary
        attFilePath.append(absHtmlFileName)
        with open(absHtmlFileName, 'r+') as f:  #将htmlSummary(即摘要信息)追加到文件头
            old = f.read()
            f.seek(0)
            f.write(htmlSummary)
            f.write(old)

        ## nginx Error Log
        htmlFileName = 'nginxErrorLog' + '_' + str(datetime.date.today()) + '.html'  # 针对异常状态码的nginx的access日志重命名
        currentDir = os.getcwd()
        absHtmlFileName = os.path.join(currentDir, 'htmlFile', htmlFileName)
        print ('生成HTML文件的绝对路径为:', absHtmlFileName)
        if os.path.isfile(absHtmlFileName):
            os.remove(absHtmlFileName)
        queryString = 'message:*\[error\]* AND NOT message:*\[lua\]*'
        queryCondition = get_queryCondition(queryString, startTime, endTime, size)
        packageDic = apiPartialSearch.apiPartialSearch(nginxErrorReponame, queryCondition)
        htmlDeatil = '<div style="font-size:14px">'
        htmlSummary = '<p></p><table border=1><tr><td>应用名称</td><td>错误日志条数</td></tr>'
        htmlSummary = htmlSummary + '<tr><td><a>%s</a></td><td>%s</td></tr>' % ('nginx 错误日志条数', packageDic['total'])
        if packageDic['total'] > 0:
            htmlDeatil = htmlDeatil + '<h4>nginx错误日志明细(共产生<span style="color:red; font-weight:bold;">%s</span>条错误日志，注:如超过5000条则只显示前5000条)</h4><ul>' % (packageDic['total'])
            for row in packageDic['hits']:
                lineLog = r'<li style="list-style: none; border-bottom: #ccc 1px dotted;">' \
                          '%s' \
                          '%s' \
                          '%s ' \
                          '</li>' % (
                              str(datetime.datetime.strptime(row.get('time_local', '-')[0:19], '%Y-%m-%dT%H:%M:%S') + datetime.timedelta(hours=8)), \
                              row.get('message', '-'), \
                              row.get('localip', '-'), \
                          )
                htmlDeatil = htmlDeatil + lineLog
            htmlDeatil = htmlDeatil + '</ul></div>'

            with open(absHtmlFileName, 'a') as f:
                f.write(htmlDeatil)
        htmlSummary = htmlSummary + '</table>'
        allHtmlSummary = allHtmlSummary + htmlSummary
        attFilePath.append(absHtmlFileName)
        # with open(absHtmlFileName, 'r+') as f:  # 将htmlSummary(即摘要信息)追加到文件头
        #     old = f.read()
        #     f.seek(0)
        #     f.write(htmlSummary)
        #     f.write(old)
    else:   #java应用日志
        for i in packageAbsLogPath:
            packageDic = collections.OrderedDict()
            packageDic['name'] =  i
            queryString = 'datasource:"%s" AND loglevel:"ERROR"' % (i)
            queryCondition = get_queryCondition(queryString, startTime, endTime, size)
            for k,v in apiPartialSearch.apiPartialSearch(appLicationReponame, queryCondition).items():
                packageDic[k] = v
            htmlDeatil = '<div id="%s" style="font-size:14px">' % i
            htmlSummary = htmlSummary + '<tr><td><a href="#%s">%s</a></td><td>%s</td></tr>' % (i, i, packageDic['total'])
            if packageDic['total'] > 0:
                htmlDeatil = htmlDeatil + '<h4>%s错误日志明细(共产生<span style="color:red; font-weight:bold;">%s</span>条错误日志，注:如超过5000条则只显示前5000条)</h4><ul>' % (packageDic['name'], packageDic['total'])
                for row in packageDic['hits']:
                    lineLog = r'<li style="list-style: none; border-bottom: #ccc 1px dotted;">' \
                              '%s %s ' \
                              '<span style="color: red;">%s</span> %s %s ' \
                              '</li>' % (
                                  str(datetime.datetime.strptime(row.get('time_local', '-')[0:19], '%Y-%m-%dT%H:%M:%S') + datetime.timedelta(hours=8)), row.get('thread', '-'), \
                                  row.get('loglevel', '-'), row.get('message', '-'), row.get('localip', '-')
                              )
                    htmlDeatil = htmlDeatil + lineLog
                htmlDeatil = htmlDeatil + '</ul></div>'
            else:
                htmlDeatil = htmlDeatil + '<h4>%s未产生错误日志Good!!!</h4></div>' % (packageDic['name'])
            with open(absHtmlFileName, 'a') as f:
                f.write(htmlDeatil)
        htmlSummary = htmlSummary + '</table>'
        attFilePath.append(absHtmlFileName)
        with open(absHtmlFileName, 'r+') as f:  #将htmlSummary(即摘要信息)追加到文件头
            old = f.read()
            f.seek(0)
            f.write(htmlSummary)
            f.write(old)
        allHtmlSummary =  allHtmlSummary + htmlSummary
    allHtmlSummary = allHtmlSummary + '</div>'

print (len(allHtmlSummary),allHtmlSummary)
print (len(attFilePath),attFilePath)
print ('处理耗时为:',time.time() - beginTime)
print ('开始发送邮件')
#sendEmailObj = sendEmailClass.sendEmallClass(host, user, passwd)
#sendEmailObj.doSendEmall(toList=['mason@yunlai.cn'], ccList=[''], subject='应用错误日志每日报告', content=allHtmlSummary)

msg = MIMEMultipart("related")
msgtext = MIMEText(allHtmlSummary, "html", "utf-8")
msg.attach(msgtext)
for i in attFilePath:
    print (i)
    attach = MIMEText(open(i, 'rb').read(), 'base64', 'utf-8')
    attach["Content-Type"] = "application/octet-stream"
    fileName = os.path.split(i)[1]
    print (fileName)
    attach["Content-Disposition"] = "attachment; filename=%s".decode("utf-8").encode("gb18030") % fileName
    msg.attach(attach)
msg["Subject"] = u'应用错误日志每日报表—生产环镜'
msg["From"] = emallUser
toList = ','.join(toList)
msg["To"] = toList

try:
    server = smtplib.SMTP()
    server.connect(emallHost,"25")
    server.starttls()
    server.login(emallUser, emallPasswd)
    server.sendmail(emallUser, toList, msg.as_string())
    server.quit()
    print (u"邮件发送成功！")
except Exception as e:
    print (u"失败",str(e))

print ('处理耗时为:',time.time() -beginTime)







